//
//  RegisterController.m
//  DingDongXiChe
//
//  Created by xieyingze on 2017/12/31.
//  Copyright © 2017年 xieyingze. All rights reserved.
//

#import "DDRegAndLogController.h"
#import "RegisterView.h"
#import "LoginView.h"
#import "DDRegAndLogViewModel.h"
#import "DDLoadingView.h"
#import "HomeController.h"

@interface DDRegAndLogController ()
/**
 注册界面
 */
@property (nonatomic, strong) RegisterView *registerView;
/**
 登录界面
 */
@property (nonatomic, strong) LoginView *loginView;
/**
 注册按钮
 */
@property (nonatomic, strong) BaseButton *registerButton;
/**
 登录按钮
 */
@property (nonatomic, strong) BaseButton *loginButton;

@property (nonatomic, assign) CGFloat buttonW;

@property (nonatomic, strong) DDRegAndLogViewModel *viewModel;

@end

@implementation DDRegAndLogController

#pragma mark - *********************生命周期*********************

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - *********************基础配置*********************

- (void)configuration {
    [self hideCustomNavView];
    
    self.buttonW = kUI_WIDTH / 6;
    
    self.loginButton.selected = NO;
    self.registerButton.selected = YES;
    
    self.registerView.hidden = NO;
    self.loginView.hidden = YES;
    self.registerView.alpha = 1.0;
    self.loginView.alpha = 0.1;
    self.registerButton.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.5, 1.5);
    
    self.registerView.viewModel = self.viewModel;
    self.loginView.viewModel = self.viewModel;
}

- (void)initSubViews {
    [self.view addSubview:self.loginButton];
    [self.view addSubview:self.registerButton];
    [self.view addSubview:self.registerView];
    [self.view addSubview:self.loginView];
}

- (void)autoLayoutForSubViews {
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@64);
        make.left.equalTo(@(2 * self.buttonW));
        make.width.equalTo(@(self.buttonW));
        make.height.equalTo(@20);
    }];
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loginButton);
        make.left.equalTo(self.loginButton.mas_right);
        make.width.equalTo(self.loginButton);
        make.height.equalTo(self.loginButton);
    }];
    [self.registerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loginButton.mas_bottom).offset(20);
        make.left.right.equalTo(@0);
        make.height.equalTo(@300);
    }];
    [self.loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loginButton.mas_bottom).offset(180);
        make.left.right.equalTo(@0);
        make.height.equalTo(@300);
    }];
}

- (void)bindViewModel {
    @weakify(self);
    [[self.registerButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(BaseButton *btn) {
        @strongify(self);
        [self updateRegisterStatus:btn];
    }];
    [[self.loginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(BaseButton *btn) {
        @strongify(self);
        [self updateLoginStatus:btn];
    }];
    [self.viewModel.gotoRegisterSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self gotoRegister];
    }];
    [self.viewModel.gotoLoginSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self gotoLogin];
    }];
    [self.viewModel.gotoLoginCommand.executionSignals.switchToLatest subscribeNext:^(NSString *resultString) {
        @strongify(self);
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[resultString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
        [self gotoMainCtrl:dictionary];
    }];
    [self.viewModel.gotoRegisterCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self updateLoginStatus:self.loginButton];
    }];
}

#pragma mark - *********************基础方法*********************

#pragma mark - *********************代理方法*********************

#pragma mark - *********************响应事件*********************

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

/**
 更新注册界面
 */
- (void)updateRegisterStatus:(BaseButton *)btn {
    self.loginButton.selected = NO;
    self.loginButton.transform = CGAffineTransformIdentity;
    if (btn.selected) {
        return;
    }
    btn.selected = !btn.selected;
    btn.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.5, 1.5);
    [self animationFrom:self.loginView to:self.registerView];
}

/**
 更新登录界面
 */
- (void)updateLoginStatus:(BaseButton *)btn {
    self.registerButton.selected = NO;
    self.registerButton.transform = CGAffineTransformIdentity;
    if (btn.selected) {
        return;
    }
    btn.selected = !btn.selected;
    btn.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.5, 1.5);
    [self animationFrom:self.registerView to:self.loginView];
}

/**
 注册登录界面切换动画
 
 @param view1 即将消失的界面
 @param view2 即将显示的界面
 */
- (void)animationFrom:(BaseView *)view1 to:(BaseView *)view2 {
    [view1 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loginButton.mas_bottom).offset(180);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        view1.alpha = 0.1;
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        view1.hidden = YES;
        view2.hidden = NO;
        [view2 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.loginButton.mas_bottom).offset(20);
        }];
        [UIView animateWithDuration:0.25 animations:^{
            view2.alpha = 1.0;
            [self.view layoutIfNeeded];
        } completion:^(BOOL finished) {
            
        }];
    }];
}

- (void)gotoRegister {
    [self.view endEditing:YES];
    [self.viewModel.gotoRegisterCommand execute:nil];
}

- (void)gotoLogin {
    [self.view endEditing:YES];
    [self.viewModel.gotoLoginCommand execute:nil];
}

- (void)gotoMainCtrl:(NSDictionary *)dic {
    DDAccount *account = [DDAccount AccountStatusWithDictionary:dic];
    [DDAcountTool saveAccount:account];
    [UnityTool setStringValue:dic[@"user_uuid"] forKey:kUSER_userId];
    [UnityTool setStringValue:dic[@"user_phone"] forKey:kUSER_phone];
    [[AppDelegate shareIntacenDelegate] setRootViewController:[[UINavigationController alloc] initWithRootViewController:[HomeController new]]];
}

#pragma mark - *********************懒加载*********************

- (RegisterView *)registerView {
    if (!_registerView) {
        _registerView = [RegisterView new];
    }
    return _registerView;
}

- (LoginView *)loginView {
    if (!_loginView) {
        _loginView = [LoginView new];
    }
    return _loginView;
}

- (BaseButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [BaseButton new];
        [_loginButton setButtonStyle:@"登录" titleColor:KCOLOR_font_light titleFont:kFONT_HelBold(14) texrAlignment:NSTextAlignmentCenter];
        [_loginButton setTitleColor:KCOLOR_font_light forState:UIControlStateNormal];
        [_loginButton setTitleColor:KCOLOR_mainColor forState:UIControlStateSelected];
    }
    return _loginButton;
}

- (BaseButton *)registerButton {
    if (!_registerButton) {
        _registerButton = [BaseButton new];
        [_registerButton setButtonStyle:@"注册" titleColor:KCOLOR_mainColor titleFont:kFONT_HelBold(14) texrAlignment:NSTextAlignmentCenter];
        [_registerButton setTitleColor:KCOLOR_font_light forState:UIControlStateNormal];
        [_registerButton setTitleColor:KCOLOR_mainColor forState:UIControlStateSelected];
    }
    return _registerButton;
}

- (DDRegAndLogViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [DDRegAndLogViewModel new];
    }
    return _viewModel;
}

@end
